package com.blds.mc.controller;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.blds.mc.common.ConstCode;
import com.blds.mc.entity.User;
import com.blds.mc.service.UserService;
import com.blds.mc.util.AESDecodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;

@RestController
@RequestMapping("user")
@Slf4j
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private WxMaService wxMaService;

    /**
     * 超级管理员申请
     * @return
     */
    @GetMapping("beAdmin")
    public boolean beAdmin(String encryptedData, String iv, HttpSession session) {
        String openId = session.getAttribute(ConstCode.SESSION_KEY_WX_MA_OPENID) == null ? "" : session.getAttribute(ConstCode.SESSION_KEY_WX_MA_OPENID).toString();
        String sessionKey = session.getAttribute(ConstCode.SESSION_KEY_WX_MA_SESSION_KEY) == null ? "" : session.getAttribute(ConstCode.SESSION_KEY_WX_MA_SESSION_KEY).toString();

        if(!StringUtils.hasText(openId) || !StringUtils.hasText(sessionKey)) {
            log.error("openId或者sessionKey为空");
            return false;
        }
        WxMaPhoneNumberInfo phone = wxMaService.getUserService().getPhoneNoInfo(sessionKey, encryptedData, iv);

        if(phone == null) {
            log.error("未获取到手机号");
            return false;
        }
        User user = new User();
        user.setOpenId(openId);
        user.setApplyFlag(1);
        user.setApplyTime(new Date());
        user.setMobile(phone.getPurePhoneNumber());

        return userService.updateById(user);
    }

    /**
     * 管理员申请
     * @return
     */
    @GetMapping("canUpload")
    public boolean canUpload(String encryptedData, String iv, HttpSession session) throws Exception {
        String openId = session.getAttribute(ConstCode.SESSION_KEY_WX_MA_OPENID) == null ? "" : session.getAttribute(ConstCode.SESSION_KEY_WX_MA_OPENID).toString();
        String sessionKey = session.getAttribute(ConstCode.SESSION_KEY_WX_MA_SESSION_KEY) == null ? "" : session.getAttribute(ConstCode.SESSION_KEY_WX_MA_SESSION_KEY).toString();

        if(!StringUtils.hasText(openId) || !StringUtils.hasText(sessionKey)) {
            log.error("openId或者sessionKey为空");
            return false;
        }
        byte[] key = Base64.decodeBase64(sessionKey);
        byte[] ivByte = Base64.decodeBase64(iv);
        byte[] encData = Base64.decodeBase64(encryptedData);
        WxMaPhoneNumberInfo phone = WxMaPhoneNumberInfo.fromJson(AESDecodeUtils.decrypt(key, ivByte, encData));

        if(phone == null) {
            log.error("未获取到手机号");
            return false;
        }

        User user = new User();
        user.setOpenId(openId);
        user.setApplyFlag(2);
        user.setApplyTime(new Date());
        user.setMobile(phone.getPurePhoneNumber());

        return userService.updateById(user);
    }

    /**
     * 申请数目
     * @return
     */
    @GetMapping("countApply")
    public Integer countApply() {
        EntityWrapper<User> userEntityWrapper = new EntityWrapper<User>();
        userEntityWrapper.setEntity(new User());
        userEntityWrapper.where("apply_flag != 0");

        return userService.selectCount(userEntityWrapper);
    }
}
